import Vue from 'vue';
import { getUser } from '@/utils/user-util';

/**
 * 定义按钮权限指令
 *
 * 根据权限显隐按钮
 * v-authCode.show="B_0151"
 * 根据权限禁用按钮
 * v-authCode="B_0151"
 */
Vue.directive('authCode', {
    bind: function(el, binding) {
        const resources = getUser().resources;
        const value = binding.value || binding.expression;
        // const disable = binding.modifiers.disable
        const show = binding.modifiers.show;

        if (!value || !resources || !resources.length) {
            return;
        }
        const itemValue = resources.find(item => {
            return item.resourceCode === value;
        });

        // 开发使用
        if (el && el.addEventListener) {
            el.addEventListener('click', () => {
                if (process.env.NODE_ENV === 'development') {
                    if (show) {
                        console.log('权限-show', value);
                    } else {
                        console.log('权限-disabled', value);
                    }
                }
            });
        }

        if (!itemValue) {
            if (show) {
                el.style.display = 'none';
            } else {
                el.disabled = true;
                el.classList.add('is-disabled');
            }
        }
    },
});
